package com.xuebusi.toutiao.admin.api.comment.controller;

import com.xuebusi.toutiao.admin.api.article.service.ArticleService;
import com.xuebusi.toutiao.admin.api.article.vo.ArtCommentStatusRVO;
import com.xuebusi.toutiao.admin.api.comment.entity.CommentEntity;
import com.xuebusi.toutiao.admin.api.comment.service.CommentService;
import com.xuebusi.toutiao.admin.api.comment.vo.*;
import com.xuebusi.toutiao.admin.api.common.annotation.LoginUser;
import com.xuebusi.toutiao.admin.api.common.result.ApiResult;
import com.xuebusi.toutiao.admin.api.common.result.PageInfo;
import com.xuebusi.toutiao.admin.api.user.entity.UserEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 评论服务控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/mp/v1_0")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private ArticleService articleService;

    /**
     * 查询评论列表
     *
     * @param commentQuery 查询参数
     * @return
     */
    @GetMapping("/comments")
    public ResponseEntity<ApiResult<PageInfo<CommentEntity>>> getComments(@Valid CommentQueryPVO commentQuery) {
        return commentService.getComments(commentQuery);
    }

    /**
     * 创建评论
     *
     * @param loginUser 登录用户
     * @param createPVO 评论表单
     * @return
     */
    @PostMapping("/comments")
    public ResponseEntity<ApiResult<CommentCreateRVO>> createComment(
            @LoginUser UserEntity loginUser, @RequestBody CommentCreatePVO createPVO) {
        return commentService.createComment(loginUser.getId(), createPVO);
    }

    /**
     * 修改评论状态
     *
     * @param article_id 文章id
     * @param statusPVO  修改状态参数
     * @return
     */
    @PutMapping("/comments/status")
    public ResponseEntity<ApiResult<ArtCommentStatusRVO>> updateCommentStatus(
            @RequestParam("article_id") Integer article_id,
            @Valid @RequestBody CommentStatusPVO statusPVO) {
        return articleService.updateCommentStatus(Integer.valueOf(article_id), statusPVO.getAllow_comment());
    }

    /**
     * 对评论或回复点赞
     *
     * @param loginUser 登录用户
     * @param likingPVO 点赞信息
     * @return
     */
    @PostMapping("/comment/likings")
    public ResponseEntity<ApiResult<CommentLikingRVO>> liking(
            @LoginUser UserEntity loginUser, @Valid @RequestBody CommentLikingPVO likingPVO) {
        return commentService.liking(loginUser.getId(), likingPVO.getTarget());
    }

    /**
     * 取消对评论或回复的点赞
     *
     * @param loginUser 登录用户
     * @param target    要取消点赞的评论id或评论回复id
     * @return
     */
    @DeleteMapping("/comment/likings/{target}")
    public ResponseEntity cancelLiking(@LoginUser UserEntity loginUser, @PathVariable("target") Integer target) {
        return commentService.cancelLiking(loginUser.getId(), target);
    }

    /**
     * 删除评论或回复
     *
     * @param loginUser 登录用户
     * @param target    评论或回复 id
     * @return
     */
    @DeleteMapping("/comments/{target}")
    public ResponseEntity deleteComment(@LoginUser UserEntity loginUser, @PathVariable("target") Integer target) {
        return commentService.deleteComment(loginUser.getId(), target);
    }

    /**
     * 修改评论置顶
     *
     * @param loginUser 登录用户
     * @param target    评论或回复id
     * @param stickyPVO 是否置顶
     * @return
     */
    @PutMapping("/comments/{target}/sticky")
    public ResponseEntity<ApiResult<CommentStickyRVO>> updateIsTop(
            @LoginUser UserEntity loginUser,
            @PathVariable("target") Integer target,
            @RequestBody @Valid CommentStickyPVO stickyPVO) {
        return commentService.updateIsTop(loginUser.getId(), target, stickyPVO.getSticky());
    }
}
